//
// Created by mac on 2019/6/14.
// Copyright (c) 2019 Facebook. All rights reserved.
//

#ifdef DEBUG
#define NSLog(...) NSLog(__VA_ARGS__)
#define debugMethod() NSLog(@"%s", __func__)
#else
#define NSLog(...)
#define debugMethod()
#endif

#import "RND2dPlayer.h"

@interface RND2dPlayer()

@end;

@implementation RND2dPlayer {
    BOOL playing;
    BOOL stopped;
    BOOL hasVideoOut;
}

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupEvents];
    }
    return self;
}

-(void) setupEvents {
    self.d2dPlayer.didPlay = ^{
        playing = YES;
        if(self.onPlaying){
            self.onPlaying(self.eventData);
        }
    };
    self.d2dPlayer.didPause = ^{
        playing = NO;
        stopped = YES;
        if(self.onStopped){
            self.onStopped(self.eventData);
        }
    };
    self.d2dPlayer.didVout = ^{
        hasVideoOut = YES;
        if(self.onVout){
            self.onVout(self.eventData);
        }
    };
}

- (void)reactSetFrame:(CGRect)frame {
    NSLog(@"改变尺寸");
    [super reactSetFrame: frame];
    [self addSubViews];
}

- (void)addSubViews{
    [self addSubview:self.d2dPlayer];
}

-(void) play {
    [self.d2dPlayer play];
}

-(void) pause {
    [self.d2dPlayer pause];
}
-(void) mute:(BOOL)muted {
    [self.d2dPlayer mute:muted];
}

-(BOOL) takeSnapshot:(NSString*)path {
    return [self.d2dPlayer takeSnapshot:path];
}

- (D2dPlayer*)d2dPlayer{
    CGRect frame = self.bounds;
    if(!_d2dPlayer){
        _d2dPlayer = [[D2dPlayer alloc] initWithFrame:frame];
        [self play];
    }else{
        [_d2dPlayer setFrame:frame];
    }
    return _d2dPlayer;
}

- (NSDictionary*)eventData{
    return @{
            @"playing": [NSNumber numberWithBool:playing],
            @"stopped": [NSNumber numberWithBool:stopped],
            @"hasVideoOut": [NSNumber numberWithBool:hasVideoOut],
    };
}
@end